package jrmob.yonyou.com.commonlib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;


import java.util.List;

import jrmob.yonyou.com.commonlib.app_config_tool.constant.Constant;

/**
 * 登录成功后进行广播，启动符合标示的页面（针对模块儿开发）
 *
 * @author zhangguang
 */
public class OpenAppReceiver_lib extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        //LogUtils.e("f");

        // 移动审批
        if (intent.getAction().equals(Constant.approve_receiver) ||
                intent.getAction().equals(Constant.trends_receiver) ||
                intent.getAction().equals(Constant.message_receiver) ||
                intent.getAction().equals(Constant.contacts_receiver) ||
                intent.getAction().equals(Constant.application_receiver) ||
                intent.getAction().equals(Constant.mini_receiver) ||
                intent.getAction().equals(Constant.task_receiver) ||
                intent.getAction().equals(Constant.app_receiver)
                ) {


            Bundle bundle1 = intent.getExtras();
            String packagename = bundle1.getString(Constant.packagename, "");
            String openactivity = bundle1.getString(Constant.openactivity, "");


            // 任务管理 cest 正定
//            packagename = "com.yonyou.jrmob.taskmanager";
//            openactivity = "com.yonyou.jrmob.taskmanager.activity.AttendanceCenterActivity_task";


            if (TextUtils.isEmpty(packagename)) {
                Toast.makeText(context, "要打开的package为空，请检查", Toast.LENGTH_LONG).show();
                return;
            }

            // 启动activity
            Intent intent1 = new Intent();
            intent1.setClassName(packagename, openactivity);//此处指定系统自带浏览器包名和Activity名称.

            Bundle bundle = new Bundle();
            bundle.putString("key1", "value1");
            bundle.putString("key2", "value2");
            bundle.putString("key3", "value3");
            intent1.putExtras(bundle);
            context.startActivity(intent1);


        } else {

            if (isAvilible(context, "com.yonyou.jrmob")) {
                //LogUtils.e("接受到了jrmob广播");
                Bundle bundle1 = intent.getExtras();
                String packagename = bundle1.getString(Constant.packagename, "");
                String openactivity = bundle1.getString(Constant.openactivity, "");

                // 启动activity
                Intent intent1 = new Intent();
                intent1.setClassName(packagename, openactivity);//此处指定系统自带浏览器包名和Activity名称.

                Bundle bundle = new Bundle();
                bundle.putString("key1", "app");
                intent1.putExtras(bundle);
                context.startActivity(intent1);

            }

        }

    }


    /**
     * 判断特定应用是否安装
     *
     * @param context
     * @param packageName
     * @return
     */
    private boolean isAvilible(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (pinfo.get(i).packageName.equalsIgnoreCase(packageName))
                return true;
        }
        return false;
    }


}